<? 
	include('base.php');
	User::protect();
	include_class('links');
	$section = 'links';

	$category_id = $_GET['category_id'];
	$id          = $_GET['id'];

	$lc = LinkCategory::get($category_id);
	$l  = Link::get($id);
	
	if (!db::isError($l)) { 
		switch($_GET['task']) {
			case 'update':
				$res = $l->update($_POST);
				if (!db::isError($res)) {
					header('Location: link_edit.php?category_id=' . $category_id . '&id=' . $id);
				}
				break;
			case 'deactivate':
				$res = $l->deactivate();
				if (!db::isError($res)) {
					header('Location: link_edit.php?category_id=' . $category_id . '&id=' . $id);
				}
				break;
			case 'activate':
				$res = $l->activate();
				if (!db::isError($res)) {
					header('Location: link_edit.php?category_id=' . $category_id . '&id=' . $id);
				}
				break;
			case 'delete':
				$res = $l->remove();
				if (!db::isError($res)) {
					header('Location: links.php?category_id=' . $category_id);
				}
				break;
		}
	}

	$editors = array('description');
	$page_title = 'Edit Link';
	include('layout/header.php');
?>

<div id="breadcrumb">
	<a href="index.php">Audition &#62;</a>
	<a href="links.php">Links &#62;</a>

	<?
	$breadcrumb = $lc->getCategoryTrail();

	if ($breadcrumb) { 
		foreach($breadcrumb as $lcBC) {
			echo('<a href="links.php?category_id=' . $lcBC->getID() . '">' . $lcBC->getName() . ' &#62;</a> ');
		}
	}
	if ($lc->error) { ?>
		<br><span class="error">
			<?= $lc->error ?>
		</span> <?
	}
	?>

	View/Edit Link
</div>

<? 	if (!db::isError($l)) {
	
	if (db::isError($res)) {
		$res->outputList();
	}
	
	if ($_GET['task'] == 'edit' || $_GET['task'] == 'update' && User::isAdmin()) { 
			
		
		$name = ($_POST) ? $_POST['name'] : $l->getName();
		$url = ($_POST) ? $_POST['url'] : $l->getURL();
		$description = ($_POST) ? $_POST['description'] : $l->getDescription();
	?>
		
		<h1>edit link:</h1>
		<div class="inset">
		<form id="edit_entry" action="<?= $PHP_SELF ?>?category_id=<?= $category_id ?>&amp;id=<?= $id ?>&amp;task=update" method="post">
		<div class="edit-form">
			<strong>Name</strong><br/>
				<input type="text" name="name" value="<?= $name ?>" class="text">
		</div>
		<div class="edit-form">
			<strong>URL</strong><br/>
			<input type="text" name="url" value="<?= $url ?>" class="text">
		</div>
		<div class="edit-form">
			<strong>Description</strong><br/>
			<textarea name="description" class="full" id="description"><?= $description ?></textarea>
		</div>
		<div class="edit-form">
			<input type="submit" value="Update Link" class="button" name="submit">
			<input type="submit" value="Exit Without Updating" class="button" name="preview" onclick="location.href='<?= $PHP_SELF ?>?category_id=<?= $category_id ?>&id=<?= $id ?>'; return false">
		</div>
		</div>

	<? } else { ?>

		<h1>view link:</h1>
		<div class="inset">
		
		<h2><?
			echo($l->getName());
		?></h2>
		
		<span class="small">
			Status: <strong><? if ($l->isActive()) { ?>Active<? } else { ?>Inactive<? } ?></strong>
		</span>
		<br/><br/>
		
		<strong>URL</strong><br>
		<?= $l->getURL() ?>
		<br/><br/>
		
		<strong>Description</strong><br/>
		<? if ($l->getDescription()) {
			echo($l->getDescription());
		} else {
			echo("none");
		} ?>
		<br/><br/>
		
		<form name="entry_status" method="get">
			<? if (User::isAdmin()) { ?>
				<input type="submit" name="" value="Edit Link" onclick="location.href='<?= $PHP_SELF ?>?category_id=<?= $category_id ?>&id=<?= $id ?>&task=edit';return false" class="button">

				<? if ($l->isActive()) { ?>
					<input type="submit" name="" value="Deactivate" onclick="location.href='<?= $PHP_SELF ?>?category_id=<?= $category_id ?>&id=<?= $id ?>&task=deactivate';return false" class="button">
				<? } else { ?>
					<input type="submit" name="" value="Activate" onclick="location.href='<?= $PHP_SELF ?>?category_id=<?= $category_id ?>&id=<?= $id ?>&task=activate';return false" class="button">
				<? } ?>

				<input type="submit" value="Remove" name="" onclick="if (confirm('Are you sure you wish to delete this link?\rWhenever possible, deactivate rather than delete.')) { location.href='<?= $PHP_SELF ?>?category_id=<?= $category_id ?>&id=<?= $id ?>&task=delete'};return false" class="button">
			
			<? } ?>
			<input type="submit" value="Return to Category" name="" onclick="location.href='links.php?category_id=<?= $category_id ?>'; return false" class="button">
		</form>
		
		</div>
	<? } ?>

<? } else { ?>
		<?=$l->outputDialog('Return to Links','links.php')?>
<? } ?>

<? include('layout/footer.php'); ?>
